<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
	<!-- toastr -->
	<script type="text/javascript" src="/js/toastr/toastr.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/js/toastr/toastr.css"/>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border" id="divId">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="delTypeTemplate()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  >									
									<button class="btn btn-default">查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="contextId">
			                          <!--<tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>手机</td>
									      <td>联想,华为,中兴</td>
									      <td>网络制式</td>
									      <td></td>		                                                                  
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" placeholder="商品类型" id="nameId"><input type="hidden" id="Ttid">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brandId" name="brandId" style="width:100%" multiple>
							<option th:each="b : ${brands}" th:value="${b.id}">[[${b.name}]]</option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="specificationId" name="specificationId" style="width:100%" multiple>
							<option th:each="s : ${specifications}" th:value="${s.id}">[[${s.specName}]]</option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性" onclick="addTr()"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="tableId">
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除" onclick="delTr(this)"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addTypeTemplate()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>


  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="updateModalLabel">修改商品类型模板</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <td><input  class="form-control" placeholder="商品类型" id="nameId2">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select name="brandId" id="brandId2" style="width:100%" multiple>
								  <option th:each="b : ${brands}" th:value="${b.id}">[[${b.name}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select name="specificationId" id="specificationId2" style="width:100%" multiple>
								  <option th:each="s : ${specifications}" th:value="${s.id}">[[${s.specName}]]</option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性" onclick="addTr()"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="tableId2">
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" class="btn btn-default" title="删除" onclick="delTr(this)"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateTypeTemplate()">修改</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
<script>
	$(function(){
		$.ajax({
			url:"/typeTemplate/findTypeTemplateAll",
			type:"post",
			dataType:"json",
			data:{},
			async:false,//同步
			success:function(result){
				if(result.code != 200){//后台查询失败
					toastr.error(result.message);
					return;
				}
				var data = result.data;	//获取后台查询全部模板信息的结果list
				$(data).each(function(i,e){
					//将每条模板信息的品牌字符串数据 转化成品牌对象
					var brands = JSON.parse(e.brandIds);//这又是一个品牌对象集合
					var specIds = JSON.parse(e.specIds);//这又是一个规格对象集合
					var customAttributeItems = JSON.parse(e.customAttributeItems);//拓展属性对象集合
					//拼接所有品牌名称名
					var brandNames = "";
					$(brands).each(function(i,e){
						brandNames += e.text + "，";
					})
					//拼接所有规格选项名
					var specNames = "";
					$(specIds).each(function(i,e){
						specNames += e.text + "，";
					})
					//拼接所有拓展属性名
					var customAttributeItemsNames = "";
					$(customAttributeItems).each(function(i,e){
						customAttributeItemsNames += e.text + "，";
					})
					//拼接数据 追加到table表格
					$("#contextId").append('<tr><td><input type="checkbox"></td><td>'+e.id+'</td><td>'+e.name+'</td><td>'+brandNames+'</td><td>'+specNames+'</td><td>'+customAttributeItemsNames+'</td><td class="text-center"><button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="initUpdatePage(this)">修改</button></td></tr>');
				})
			},
			error:function(){
				toastr.error("系统错误");
			}
		})
	})

	//页面初始化后，将下拉框编程多选的下拉框
	$(function(){
		$("[name=brandId]").select2();
		$("[name=specificationId]").select2();
	})

	//增加拓展属性tr
	function addTr(){
		$("#tableId").append('<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td><td><input class="form-control" placeholder="属性名称" ></td><td><button type="button" class="btn btn-default" title="删除" onclick="delTr(this)"><i class="fa fa-trash-o"></i> 删除</button></td></tr>')
		$("#tableId2").append('<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td><td><input class="form-control" placeholder="属性名称" ></td><td><button type="button" class="btn btn-default" title="删除" onclick="delTr(this)"><i class="fa fa-trash-o"></i> 删除</button></td></tr>')
	}

	//删除拓展属性tr
	function delTr(obj){
		var tr = obj.parentNode.parentNode;
		tr.remove();
	}

	//添加类型模板
	function addTypeTemplate(){
		//获取商品类型的名称
		var name = $("#nameId").val();

		//获取多选下拉框所选中的对象集合
		var brands = $("#brandId").select2('data');
		//品牌
		var brandList = [];
		$(brands).each(function(i,e){
			var brand = {id:e.id,text:e.text};
			brandList.push(brand);
		})
		//规格
		var specifications = $("#specificationId").select2('data');
		var specificationList = [];
		$(specifications).each(function(i,e){
			var specification = {id:e.id,text:e.text};
			specificationList.push(specification);
		})

		//获取拓展属性填入的值
		var customAttributeItemsList = [];
		$("#tableId").find("tr").each(function(i,e){	//获取table下面所有的tr
			var tds = $(this).children();	//获取当前tr下面所有的td集合
			var customAttributeItems = tds.eq(1).find("input").val();	//获取当前tr下面第2个td(从0开始)中input的value值
			var obj = {text:customAttributeItems};
			customAttributeItemsList.push(obj);
		})

		//通过ajax传递到后台进行增加分类模板
		$.ajax({
			url:"/typeTemplate/addTypeTemplate",
			type:"post",
			dataType:"json",
			data:{name:name,brandIds:JSON.stringify(brandList),specIds:JSON.stringify(specificationList),customAttributeItems:JSON.stringify(customAttributeItemsList)},
			async:false,
			success:function(result){
				toastr.success("增加成功");
				window.location.reload();
			},
			error:function(){
				toastr.error("系统错误");
			}
		})

	}

	//删除类型模板
	function delTypeTemplate(){
		//获取选中的模板id复选框
		var checkedTt = $("input[type=checkbox]:checked");
		var ids = [];//选中要删除的分类模板id数组
		$(checkedTt).each(function(i,e){	//获取每个checked的input元素
			//获取他父节点td
			var tdParent = $(e).parent();
			//获取当前节点的下一个兄弟节点
			var tdNext = tdParent.next();
			//获取当前节点中的值
			var id = tdNext.html();
			ids.push(id);
		})
		if(ids.length <= 0){
			toastr.warning("请选择需要删除的分类模板");
			return;
		}

		//通过ajax传递到后台进行删除
		$.ajax({
			url:"/typeTemplate/deleteTypeTemplateByIds",
			type:"post",
			dataType:"json",
			data:{ids:ids},
			async:false,
			success:function(){
				toastr.success("删除成功");
				window.location.reload();
			},
			error:function(){
				toastr.error("系统错误");
			}
		})

	}

	//初始化修改页面的信息 该模板的信息
	function initUpdatePage(obj){
		//获取当前选中的分类模板id
		var tr = $(obj).parent().parent();
		var tds = $(tr).children();
		var td = tds.eq(1);
		var id = td.html();

		//通过ajax去后台查询该分类的数据
		$.ajax({
			url:"/typeTemplate/getTypeTemplateById",
			type:"post",
			dataType:"json",
			data:{id:id},
			async:false,
			success:function(result){
				var data = result.data;
				//类型名称
				$("#nameId2").val(data.name);
				//id值
				$("#Ttid").val(data.id);

				//关联品牌
				var brandList = JSON.parse(data.brandIds);
				var brandIds = [];
				$(brandList).each(function(i,e){
					brandIds.push(e.id);
				})
				$("#brandId2").val(brandIds).trigger("change");

				//关联规格
				var specList = JSON.parse(data.specIds);
				var specIds = [];
				$(specList).each(function(i,e){
					specIds.push(e.id);
				})
				$("#specificationId2").val(specIds).trigger("change");

				//拓展属性
				var customAttributeItems = JSON.parse(data.customAttributeItems);
				$("#tableId2").html("");
				$(customAttributeItems).each(function(i,e){
					$("#tableId2").append('<tr><td><input type="checkbox" class="icheckbox_square-blue" ></td><td><input class="form-control" placeholder="属性名称" value="'+e.text+'"></td><td><button type="button" class="btn btn-default" title="删除" onclick="delTr(this)"><i class="fa fa-trash-o"></i> 删除</button></td></tr>');
				})
			},
			error:function(){
				toastr.error("系统错误");
			}
		})
	}


	//修改分类模板
	function updateTypeTemplate(){
		//获取商品类型的名称
		var name = $("#nameId2").val();

		//获取多选下拉框所选中的对象集合
		var brands = $("#brandId2").select2('data');
		//品牌
		var brandList = [];
		$(brands).each(function(i,e){
			var brand = {id:e.id,text:e.text};
			brandList.push(brand);
		})
		//规格
		var specifications = $("#specificationId2").select2('data');
		var specificationList = [];
		$(specifications).each(function(i,e){
			var specification = {id:e.id,text:e.text};
			specificationList.push(specification);
		})

		//获取拓展属性填入的值
		var customAttributeItemsList = [];
		$("#tableId2").find("tr").each(function(i,e){	//获取table下面所有的tr
			var tds = $(this).children();	//获取当前tr下面所有的td集合
			var customAttributeItems = tds.eq(1).find("input").val();	//获取当前tr下面第2个td(从0开始)中input的value值
			var obj = {text:customAttributeItems};
			customAttributeItemsList.push(obj);
		})

		//通过ajax传递到后台进行增加分类模板
		$.ajax({
			url:"/typeTemplate/updateTypeTemplateById",
			type:"post",
			dataType:"json",
			data:{id:$("#Ttid").val(),name:name,brandIds:JSON.stringify(brandList),specIds:JSON.stringify(specificationList),customAttributeItems:JSON.stringify(customAttributeItemsList)},
			async:false,
			success:function(result){
				toastr.success("修改成功");
				window.location.reload();
			},
			error:function(){
				toastr.error("系统错误");
			}
		})
	}
</script>
</html>